package com.ly.mpayment.rest.system;

import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.ly.mpayment.core.util.*;
import com.ly.mpayment.persist.entity.*;
import com.ly.mpayment.persist.service.timer.ProductTimerService;
import com.ly.mpayment.persist.service.timer.ProductTimerUpService;
import com.ly.mpayment.persist.service.user.IUserService;
import com.ly.mpayment.rest.AbsRest;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Component
@Path("system/productTimer/")
public class ProductTimerRest extends AbsRest {
    @Autowired
    IUserService userService;

    @Autowired
    ProductTimerService timerService;

    @Autowired
    ProductTimerUpService timerUpService;



    @Path("queryProductTimerPage")
    @POST
    @SuppressWarnings("static-access")
    public String queryProductTimerPage(String data,@Context HttpServletRequest request){
        String result = this.validationParameterGetRole(  request, userService );
        if(result == null){
            JSONObject json = JsonUtils.getJSONOBJCT().fromObject(data);
            String token = request.getHeader("token");
            if (token != null) {
                DecodedJWT jwt;
                try {
                    jwt = JWT.decode(token);
                    if (jwt != null) {
                        JSONObject jsonToken = JsonUtils.converToJson(jwt.getSubject());
                        if (jsonToken != null) {

                            User login = userService.findByUserid(jsonToken.getString(Config.tokenSignAttr));
                            //如果是下游客户 只能导出一个月的
                            if (login != null && UserUtil.checkRole(login)) {
                                Map<String, Object> parameters = MapUtil.getMap();
                                //"select b from ProductTimer b where 1 = 1 #[ and  b.operator = :operator ]#  #[ and  b.province = :province ]# " +
                                //        " #[ and  b.createTime >= :starttime ]#  #[ and  b.enabled = :enabled ]#  #[ and  b.status = :status ]#   " +
                                //        " #[ and  b.createTime <= :endtime ]#  #[ and  b.busname = :busname ]#   #[ and  b.createUser = :createUser ]#  " +
                                //        " #[ and  b.createTime like :downName ]#  order by b.createTime desc " ;

                                if(!StringUtils.isBlank(json.getString("operator"))){
                                    parameters.put(":operator", json.getString("operator"));
                                }
                                if(!StringUtils.isBlank(json.getString("starttime"))){
                                    parameters.put(":starttime",  (json.getString("starttime")));
                                }

                                if(!StringUtils.isBlank(json.getString("endtime"))){
                                    parameters.put(":endtime",  (json.getString("endtime")));
                                }
                                if(!StringUtils.isBlank(json.getString("enabled"))){
                                    parameters.put(":enabled",json.getBoolean("enabled") );
                                }
                                if(!StringUtils.isBlank(json.getString("status"))){
                                    parameters.put(":status",json.getInt("status") );
                                }
                                if(!StringUtils.isBlank(json.getString("busname"))){
                                    parameters.put(":busname",json.getString("busname") );
                                }
                                if(!StringUtils.isBlank(json.getString("createUser"))){
                                    parameters.put(":createUser",json.getString("createUser") );
                                }
                                if(!StringUtils.isBlank(json.getString("downName"))){
                                    parameters.put(":downName",json.getString("downName") );
                                }
                                return ResultMessage.getSucceed( JsonUtils.excludes(this.timerService.queryProductTimerPage(parameters, json.getInt("start"), json.getInt("pageSize")), new String[]{"cityCode","postCode"}));
                            }
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    @Path("updateProductTimer")
    @POST
    @SuppressWarnings("static-access")
    public String updateProductTimer(String data,@Context HttpServletRequest request){
        //orderSort
        String result = this.validationParameterGetRole( request, userService);
        if(result == null){
            JSONObject json = JsonUtils.getJSONOBJCT().fromObject(data);
            String token = request.getHeader("token");
            if (token != null) {
                DecodedJWT jwt;
                try {
                    jwt = JWT.decode(token);
                    if (jwt != null) {
                        JSONObject jsonToken = JsonUtils.converToJson(jwt.getSubject());
                        if (jsonToken != null) {
                            User login = userService.findByUserid(jsonToken.getString(Config.tokenSignAttr));
                            if (login != null && UserUtil.checkRole(login)) {
                                if(this.timerService.updateProductTimer(json)){
                                    return ResultMessage.getSucceed( );
                                }
                                return ResultMessage.getFailt("");
                            }
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     * 手动执行
     * @param data
     * @param request
     * @return
     */
    @Path("executeProductTimer")
    @POST
    @SuppressWarnings("static-access")
    public String executeProductTimer(String data,@Context HttpServletRequest request){
        //orderSort
        String result = this.validationParameterGetRole( request, userService);
        if(result == null){
            JSONObject json = JsonUtils.getJSONOBJCT().fromObject(data);
            String token = request.getHeader("token");
            if (token != null) {
                DecodedJWT jwt;
                try {
                    jwt = JWT.decode(token);
                    if (jwt != null) {
                        JSONObject jsonToken = JsonUtils.converToJson(jwt.getSubject());
                        if (jsonToken != null) {
                            User login = userService.findByUserid(jsonToken.getString(Config.tokenSignAttr));
                            if (login != null && UserUtil.checkRole(login)) {
                                if(this.timerService.executeProductTimer(json.getString("id"))){
                                    return ResultMessage.getSucceed( );
                                }
                                return ResultMessage.getFailt("");
                            }
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    @Path("detailProductTimer")
    @POST
    @SuppressWarnings("static-access")
    public String detailProductTimer(String data,@Context HttpServletRequest request){
        //orderSort
        String result = this.validationParameterGetRole( request, userService);
        if(result == null){
            JSONObject json = JsonUtils.getJSONOBJCT().fromObject(data);
            String token = request.getHeader("token");
            if (token != null) {
                DecodedJWT jwt;
                try {
                    jwt = JWT.decode(token);
                    if (jwt != null) {
                        JSONObject jsonToken = JsonUtils.converToJson(jwt.getSubject());
                        if (jsonToken != null) {
                            User login = userService.findByUserid(jsonToken.getString(Config.tokenSignAttr));
                            if (login != null && UserUtil.checkRole(login)) {
                                return ResultMessage.getSucceed(  JsonUtils.convertListToJson(this.timerService.detailProductTimer(json.getString("id")),
                                        new String[]{"proId","provincialInformation","userId","operators","businessType","user","","","","","","","","",""}));
                            }
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    @Path("queryProductTimerPageUp")
    @POST
    @SuppressWarnings("static-access")
    public String queryProductTimerPageUp(String data,@Context HttpServletRequest request){
        String result = this.validationParameterGetRole(  request, userService );
        if(result == null){
            JSONObject json = JsonUtils.getJSONOBJCT().fromObject(data);
            String token = request.getHeader("token");
            if (token != null) {
                DecodedJWT jwt;
                try {
                    jwt = JWT.decode(token);
                    if (jwt != null) {
                        JSONObject jsonToken = JsonUtils.converToJson(jwt.getSubject());
                        if (jsonToken != null) {

                            User login = userService.findByUserid(jsonToken.getString(Config.tokenSignAttr));
                            //如果是下游客户 只能导出一个月的
                            if (login != null && UserUtil.checkRole(login)) {
                                Map<String, Object> parameters = MapUtil.getMap();

                                if(!StringUtils.isBlank(json.getString("operator"))){
                                    parameters.put(":operator", json.getString("operator"));
                                }
                                if(!StringUtils.isBlank(json.getString("starttime"))){
                                    parameters.put(":starttime",  (json.getString("starttime")));
                                }

                                if(!StringUtils.isBlank(json.getString("endtime"))){
                                    parameters.put(":endtime",  (json.getString("endtime")));
                                }
                                if(!StringUtils.isBlank(json.getString("enabled"))){
                                    parameters.put(":enabled",json.getBoolean("enabled") );
                                }
                                if(!StringUtils.isBlank(json.getString("status"))){
                                    parameters.put(":status",json.getInt("status") );
                                }
                                if(!StringUtils.isBlank(json.getString("busname"))){
                                    parameters.put(":busname",json.getString("busname") );
                                }
                                if(!StringUtils.isBlank(json.getString("createUser"))){
                                    parameters.put(":createUser",json.getString("createUser") );
                                }
                                if(!StringUtils.isBlank(json.getString("upName"))){
                                    parameters.put(":upName",json.getString("upName") );
                                }
                                return ResultMessage.getSucceed( JsonUtils.excludes(this.timerUpService.queryProductTimerPage(parameters, json.getInt("start"), json.getInt("pageSize")), new String[]{"cityCode","postCode"}));
                            }
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    @Path("updateProductTimerUp")
    @POST
    @SuppressWarnings("static-access")
    public String updateProductTimerUp(String data,@Context HttpServletRequest request){
        //orderSort
        String result = this.validationParameterGetRole( request, userService);
        if(result == null){
            JSONObject json = JsonUtils.getJSONOBJCT().fromObject(data);
            String token = request.getHeader("token");
            if (token != null) {
                DecodedJWT jwt;
                try {
                    jwt = JWT.decode(token);
                    if (jwt != null) {
                        JSONObject jsonToken = JsonUtils.converToJson(jwt.getSubject());
                        if (jsonToken != null) {
                            User login = userService.findByUserid(jsonToken.getString(Config.tokenSignAttr));
                            if (login != null && UserUtil.checkRole(login)) {
                                if(this.timerUpService.updateProductTimer(json)){
                                    return ResultMessage.getSucceed( );
                                }
                                return ResultMessage.getFailt("");
                            }
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     * 手动执行
     * @param data
     * @param request
     * @return
     */
    @Path("executeProductTimerUp")
    @POST
    @SuppressWarnings("static-access")
    public String executeProductTimerUp(String data,@Context HttpServletRequest request){
        //orderSort
        String result = this.validationParameterGetRole( request, userService);
        if(result == null){
            JSONObject json = JsonUtils.getJSONOBJCT().fromObject(data);
            String token = request.getHeader("token");
            if (token != null) {
                DecodedJWT jwt;
                try {
                    jwt = JWT.decode(token);
                    if (jwt != null) {
                        JSONObject jsonToken = JsonUtils.converToJson(jwt.getSubject());
                        if (jsonToken != null) {
                            User login = userService.findByUserid(jsonToken.getString(Config.tokenSignAttr));
                            if (login != null && UserUtil.checkRole(login)) {
                                if(this.timerUpService.executeProductTimer(json.getString("id"))){
                                    return ResultMessage.getSucceed( );
                                }
                                return ResultMessage.getFailt("");
                            }
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    @Path("detailProductTimerUp")
    @POST
    @SuppressWarnings("static-access")
    public String detailProductTimerUp(String data,@Context HttpServletRequest request){
        //orderSort
        String result = this.validationParameterGetRole( request, userService);
        if(result == null){
            JSONObject json = JsonUtils.getJSONOBJCT().fromObject(data);
            String token = request.getHeader("token");
            if (token != null) {
                DecodedJWT jwt;
                try {
                    jwt = JWT.decode(token);
                    if (jwt != null) {
                        JSONObject jsonToken = JsonUtils.converToJson(jwt.getSubject());
                        if (jsonToken != null) {
                            User login = userService.findByUserid(jsonToken.getString(Config.tokenSignAttr));
                            if (login != null && UserUtil.checkRole(login)) {
                                return ResultMessage.getSucceed(  JsonUtils.convertListToJson(this.timerUpService.detailProductTimer(json.getString("id")),
                                        new String[]{"proId","provincialInformation","userId","operators","businessType","user","","","","","","","","",""}));
                            }
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return result;
    }


}
